package com.xunyin.modules.mainpart.rest;

import com.xunyin.dozer.service.IGenerator;
import com.xunyin.modules.log.Log;
import com.xunyin.modules.aop.ForbidSubmit;
import com.xunyin.modules.area.param.TAreaQueryParam;
import com.xunyin.modules.area.service.TAreaSpaceService;
import com.xunyin.modules.mainpart.domain.TMainPart;
import com.xunyin.modules.mainpart.service.TMainPartService;
import com.xunyin.modules.mainpart.service.dto.TMainPartQueryCriteria;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * @Author xunyinyunshang  <3594419785@qq.com>
 * @date 2022-09-09
 */
@Api(tags = "共享空间:主体管理")
@RestController
@RequestMapping("api")
public class MainPartController {

    private final TMainPartService tMainPartService;

    private final IGenerator generator;

    @Autowired
    private TAreaSpaceService tAreaSpaceService;

    public MainPartController(TMainPartService tMainPartService, IGenerator generator) {
        this.tMainPartService = tMainPartService;
        this.generator = generator;
    }

    // YXSTOREPRODUCT_SELECT,YXSTORECATEGORY_ALL,YXSTORECATEGORY_SELECT,YXSYSTEMGROUPDATA_SELECT
    @Log("查询主体")
    @ApiOperation(value = "查询主体")
    @GetMapping(value = "/tMainPart")
    @PreAuthorize("hasAnyRole('admin','MAINPART_ALL','MAINPART_SELECT')")
    public ResponseEntity getTMainPart(TMainPartQueryCriteria criteria, Pageable pageable) {
        Map<String, Object> queryAll = tMainPartService.queryAll(criteria, pageable);
        return new ResponseEntity<>(queryAll, HttpStatus.OK);
    }

    @ForbidSubmit
    @Log("新增主体")
    @ApiOperation(value = "新增主体")
    @PostMapping(value = "/tMainPart")
    @PreAuthorize("hasAnyRole('admin','YXSYSTEMUSERLEVEL_ALL','YXSYSTEMUSERLEVEL_CREATE')")
    public ResponseEntity create(@Validated @RequestBody TMainPart resources) {
        return new ResponseEntity<>(tMainPartService.save(resources), HttpStatus.CREATED);
    }

    @ForbidSubmit
    @Log("修改主体")
    @ApiOperation(value = "修改主体")
    @PutMapping(value = "/tMainPart")
    @PreAuthorize("hasAnyRole('admin','YXSYSTEMUSERLEVEL_ALL','YXSYSTEMUSERLEVEL_EDIT')")
    public ResponseEntity update(@Validated @RequestBody TMainPart resources) {
        tMainPartService.saveOrUpdate(resources);
        return new ResponseEntity(HttpStatus.NO_CONTENT);
    }

    @ForbidSubmit
    @Log("删除")
    @ApiOperation(value = "删除")
    @DeleteMapping(value = "/tMainPart/{id}")
    @PreAuthorize("hasAnyRole('admin','YXSYSTEMUSERLEVEL_ALL','YXSYSTEMUSERLEVEL_DELETE')")
    public ResponseEntity delete(@PathVariable Integer id) {
        tMainPartService.removeById(id);
        return new ResponseEntity(HttpStatus.OK);
    }

    @Log("区域省份、市或区县列表")
    @ApiOperation("区域省份、市或区县列表")
    @PostMapping(value = "/listAreaByCode")
    // @PreAuthorize("@el.check('yxSystemStore:list')")
    public ResponseEntity<Object> listAreaByCode(@Validated @RequestBody TAreaQueryParam areaQueryParam) {
        return new ResponseEntity<>(tAreaSpaceService.listAreaByCode(areaQueryParam.getAreaParent()), HttpStatus.OK);
    }

    @Log("获取主体列表")
    @ApiOperation("获取主体列表")
    @PostMapping(value = "/listMainPart")
    // @PreAuthorize("@el.check('yxSystemStore:list')")
    public ResponseEntity<Object> listMainPart() {
        return new ResponseEntity<>(tMainPartService.listMainPart(), HttpStatus.OK);
    }
}
